//
//  PostsDetailViewController.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/19.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "PostsDetailViewController.h"
#import "AFNetworking.h"
#import "PostsDetailViewModel.h"
#import "HomeChannelDetailModel.h"
#import "TAMacro.h"
#import "PostsDetailView.h"
#import "PersonalViewController.h"
#import "AllLikedUserViewController.h"

#import "PostDetailToolBar.h"
@interface PostsDetailViewController ()
<
    LikeButtonDelegate,
    CommentButtonDelegate,
    MoreButtonDelegate
>
@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) PostsDetailViewModel *postsDetailViewModel;
@property (nonatomic, strong) PostsDetailView *detailView;
@property (nonatomic, strong) PostDetailToolBar *toolBar;
@property (nonatomic, weak) UIScrollView *scrollView;


@end

@implementation PostsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor cyanColor];
    
    [self createView];
    [self handleNavigationBar];
}

- (void)setPostId:(NSString *)postId {
    _postId = postId;
    [self netWork];
    
}

- (void)netWork {
    NSString *userid = @"57c01d33a9a36d0621c123f7";
    NSString *text = [NSString stringWithFormat:@"{\"uid\":\"%@\",\"pid\":\"%@\"}", userid, _postId];
    //    NSLog(@"%@", text);
    NSString *utf8Text = [text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]];
    //    NSLog(@"%@", utf8Text);
    //    NSString *p = [self URLEncodedString:utf8Text];
    //    NSString *parmeters = [NSString stringWithFormat:@"c=Post&m=postDetail&p=%7B%22uid%22%3A%2257c01d33a9a36d0621c123f7%22%2C%22pid%22%3A%225723ebe4a9a36d3340a304b6%22%7D&t=t&v=v105", utf8Text];
    
    NSString *parmeters = [NSString stringWithFormat:@"c=Post&m=postDetail&p=%@&t=t&v=v105", utf8Text];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *postURLString = @"http://api.zheli.io:9501/";
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    // 设置请求参数的字符串, 自定义
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        
        return parmeters;
    }];
    // 正常post发送
    [manager POST:postURLString parameters:parmeters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        HomeChannelDetailModel *homeChannelDetailModel = [HomeChannelDetailModel modelWithDic:responseObject];
        self.postsDetailViewModel = [[PostsDetailViewModel alloc] init];
        _postsDetailViewModel.homeChannelDetailModel = homeChannelDetailModel;
        _detailView.frame = CGRectMake(0, 0, SCREEN_WIDTH, _postsDetailViewModel.detailViewHeight);

        
//        _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, 2 * SCREEN_HEIGHT);
        _scrollView.contentSize = _postsDetailViewModel.scrollViewContentView;
        _detailView.backgroundColor = [UIColor whiteColor];
        _detailView.postDetailViewModel = _postsDetailViewModel;
        _toolBar.postDetailViewModel = _postsDetailViewModel;

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败: %@", error);
    }];
    

}

- (void)createView {
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49)];
    scrollView.backgroundColor = [UIColor colorWithWhite:0.917 alpha:1];
    [self.view addSubview:scrollView];
    _scrollView = scrollView;
    
    
    self.detailView = [[PostsDetailView alloc] init];
    _detailView.backgroundColor = [UIColor whiteColor];
    
    // push到个人主页
    __weak typeof(self) weakSelf = self;
    PersonalViewController *personalVC = [[PersonalViewController alloc] init];
    _detailView.postPersonalPage = ^(NSString *ID){
        NSLog(@"%@", ID);
        [weakSelf.navigationController pushViewController:personalVC animated:YES];
        personalVC.postId = ID;
    };
    
    // push到所有点赞人的页面
    AllLikedUserViewController *allVC = [[AllLikedUserViewController alloc] init];
    _detailView.postAllPersonalPage = ^(NSString *pid) {
      
        [weakSelf.navigationController pushViewController:allVC animated:YES];
        allVC.ID = pid;
    };
    
    
    
    [_scrollView addSubview:_detailView];
    
    
    self.toolBar = [[PostDetailToolBar alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 49, SCREEN_WIDTH, 49)];
    _toolBar.backgroundColor = [UIColor whiteColor];
    self.toolBar.likeDelegate = self;
    self.toolBar.commentDelegate = self;
    self.toolBar.moreDelegate = self;
    [self.view addSubview:_toolBar];

}


- (void)handleNavigationBar {
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    _titleLabel.center = CGPointMake(SCREEN_WIDTH / 2, 42);
    _titleLabel.textAlignment = 1;
    _titleLabel.textColor = [UIColor whiteColor];
    [self.backView addSubview:_titleLabel];
    _titleLabel.text = @"帖子详情";
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 27, 30, 30)];
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [self.backView addSubview:backButton];
    [backButton addTarget:self action:@selector(popViewControllerAction:) forControlEvents:UIControlEventTouchUpInside];
}

// 返回上一页
- (void)popViewControllerAction: (UIButton *)backButton {
    [self.navigationController popViewControllerAnimated:YES];
}




#pragma mark - ToolBar的各种点击代理事件
- (void)clickLikeButton:(UIButton *)button {
    NSLog(@"点赞");
}

- (void)clickCommentButton:(UIButton *)button {
    NSLog(@"留言");
}

- (void)clickMoreButton:(UIButton *)button {
    NSLog(@"更多");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
//    [_scrollView removeFromSuperview];

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
